<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex" class="energy-alarm-report">
    <BasicTable @register="registerTable" class="w-4/4 xl:w-5/5" :searchInfo="searchInfo">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'processStatus'">
          <Tag :color="record.processStatus ? 'green' : 'red'" :class="!record.processStatus && 'link'"
            @click="handleStatusFn(record)">
            {{ record.processStatus ? '已处理' : '未处理' }}
          </Tag>
        </template>
      </template>
    </BasicTable>
    <HandleModal @register="registerModal" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts" setup>
import { reactive } from 'vue';

import { BasicTable, useTable } from '@/components/Table';
import { getTableData } from '@/api/dataReport/energyAlarmReport';
import { PageWrapper } from '@/components/Page';

import { useModal } from '@/components/Modal';
import HandleModal from './HandleModal.vue';
import { columns, searchFormSchema } from './type.data';
import { Tag } from 'ant-design-vue'

defineOptions({ name: 'AccountManagement' });

const [registerModal, { openModal }] = useModal();
const searchInfo = reactive<Recordable>({});

const [registerTable, { reload, updateTableDataRecord }] = useTable({
  api: getTableData,
  title: '能源告警报表',
  rowKey: 'id',
  columns,
  formConfig: {
    labelWidth: 120,
    schemas: searchFormSchema,
    autoSubmitOnEnter: true,
  },
  useSearchForm: true,
  showTableSetting: false,
  bordered: true,
});

const handleStatusFn = (record) => {
  const { processStatus, id } = record
  if (processStatus) return
  openModal(true, {
    id
  });
}


function handleSuccess({ isUpdate, values }) {
  if (isUpdate) {
    // 演示不刷新表格直接更新内部数据。
    // 注意：updateTableDataRecord要求表格的rowKey属性为string并且存在于每一行的record的keys中
    const result = updateTableDataRecord(values.id, values);
    console.log(result);
  } else {
    reload();
  }
}
</script>
<style lang="less" scope>
.link {
  cursor: pointer;
}
</style>
